#include "./bccodewnd.h"
#include "zxing/zxing.h"

using namespace MiniUI;
using namespace MiniUI::Type;

BCCodeWnd::BCCodeWnd()
{
    m_pWinView = WinView::CreateInstance();
    m_pWinView->SetWinBorder(MiniRect(0, 0, 0, 0));
    m_pWinView->OpenWindow(MiniRect(100, 100, 689, 546));

    Image* pBkg = Image::CreateInstance();
    pBkg->SetValue("wnd_bkg.png");
    pBkg->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
    pBkg->SetMargin(MiniRect(0, 0, 0, 0));
    pBkg->SetTransparent(true);
    m_pWinView->AddChild(pBkg);
    pBkg->Release();

    Button* pBtnClose = Button::CreateInstance();
    pBtnClose->OnClick += EventObject<OnClickEventFunc>(this, &BCCodeWnd::OnBtnCloseClick);
    pBtnClose->Init("btnclose_normal.png", "btnclose_hover.png", "btnclose_down.png");
    m_pWinView->AddChild(pBtnClose);
    pBtnClose->SetAnchors(UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT);
    pBtnClose->SetMargin(MiniRect(0, 1, 3, 0));
    pBtnClose->Release();

    m_pImgBC = Image::CreateInstance();
    m_pImgBC->SetValue("bccode1.png");
    m_pImgBC->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP);
    m_pImgBC->SetMargin(MiniRect(10, 50, 0, 0));
    m_pImgBC->SetSize(MiniSize(423, 341));
    m_pImgBC->SetTransparent(true);
    m_pWinView->AddChild(m_pImgBC);

    Button* pBtnAnalyze = Button::CreateInstance();
    pBtnAnalyze->OnClick += EventObject<OnClickEventFunc>(this, &BCCodeWnd::OnBtnAnalyzeClick);
    pBtnAnalyze->Init("mybtn_normal.png", "mybtn_hover.png", "mybtn_down.png");
    pBtnAnalyze->SetText("analyze");
    m_pWinView->AddChild(pBtnAnalyze);
    pBtnAnalyze->SetPos(MiniPoint(450, 50));
    pBtnAnalyze->SetSize(MiniSize(66, 23));
    pBtnAnalyze->Release();

    m_pTextNumber = Text::CreateInstance();
    m_pTextNumber->SetValue("---");
    m_pWinView->AddChild(m_pTextNumber);
    m_pTextNumber->SetPos(MiniPoint(450, 80));
    m_pTextNumber->SetSize(MiniSize(100, 50));
}

BCCodeWnd::~BCCodeWnd()
{
    m_pTextNumber->Release();
    m_pImgBC->Release();
    m_pWinView->Release();
}

void BCCodeWnd::OnBtnCloseClick(const UIObject* sender, const Type::MiniPoint& pt, MouseButton button, uint keyState)
{
    ::PostQuitMessage(0);
}

void BCCodeWnd::OnBtnAnalyzeClick(const UIObject* sender, const Type::MiniPoint& pt, MouseButton button, uint keyState)
{
    Bitmap* pBitmap = m_pImgBC->GetBitmap();
    char* bytes = (char*)pBitmap->GetBytes();
    MiniSize szBitmap;
    pBitmap->GetBitmapSize(szBitmap);

    char* grayData = new char[szBitmap.cx * szBitmap.cy];
    char* tmpData = grayData;
    MiniARGB* tmpColor = (MiniARGB*)bytes;
    for (int x = 0; x < szBitmap.cx; ++x)
    {
        for (int y = 0; y < szBitmap.cy; ++y)
        {
            *tmpData = (tmpColor->red * 5 + tmpColor->green * 9 + tmpColor->blue * 2) >> 4;
            tmpData++;
            tmpColor++;
        }
    }

    ZXResult result = zxDecodeBitmap((unsigned char*)grayData, szBitmap.cx, szBitmap.cy, 0, 0, szBitmap.cx, szBitmap.cy, 1 << ZXFormat_EAN_13, true);

    if (result.text)
        m_pTextNumber->SetValue(result.text);
    else
        m_pTextNumber->SetValue(result.errText);

    delete grayData;
    delete result.text;
    delete result.errText;
}